package code.accelerometro2;

import java.util.Iterator;
import java.util.LinkedList;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.XYPlot;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 
 * @author Oriana Costantino, Silvia Nunnari
 * La classe permette la visualizzazione in un grafico dei dati proveniente dall'AccelerometerActivity
 *
 */
public class Grafico extends Activity{
	
	public SensorManager mSensorManager;
	private XYPlot mySimpleXYPlot;
    private SimpleXYSeries xHistorySeries = null;
    private SimpleXYSeries yHistorySeries = null;
    private SimpleXYSeries zHistorySeries = null;
    private LinkedList<Number> xHistoryList;
    private LinkedList<Number> yHistoryList;
    private LinkedList<Number> zHistoryList;

    private Intent intent;
    private Button indietro;
   
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grafico);
       
        xHistoryList = AccelerometerActivity.xHistoryFilter;
        yHistoryList = AccelerometerActivity.yHistoryFilter;
        zHistoryList = AccelerometerActivity.zHistoryFilter;
      
       creaGrafico();
       selezioneAssi();
       
       // implementazione del button indietro, permette di tornare all'activity precedente ed quindi di effettuare una nuova misurazione
       indietro = (Button) findViewById(R.id.indietro);
       indietro.setOnClickListener(new OnClickListener() {
            public void onClick(View v)  {
            	AccelerometerActivity.resetListe();
            	finish();
            }
       }  );
     
       
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.activity_grafico, menu);
       return true;
    }
    
  /**
   * Metodo che permette la creazione del grafico che conterra' l'andamento complessivo (campionato) degli assi X Y e Z relaviti all'accelerazione
   */
    private void creaGrafico(){
    	
    	xHistorySeries = new SimpleXYSeries("AsseX");
    	yHistorySeries = new SimpleXYSeries("AsseY");
    	zHistorySeries = new SimpleXYSeries("AsseZ");
    	
    	mySimpleXYPlot = (XYPlot) findViewById(R.id.myPlotBig);
        
    	 // aggiunta delle serie che rappresentano l'andamento dei dati dell'accelerometro rispettivamente lungo gli assi X Y e Z
        mySimpleXYPlot.addSeries(xHistorySeries,  LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(100, 100, 200), Color.BLACK, null));
        mySimpleXYPlot.addSeries(yHistorySeries,  LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(100, 200, 100), Color.BLACK, null));
        mySimpleXYPlot.addSeries(zHistorySeries,  LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(200, 100, 100), Color.BLACK, null));
	
    	mySimpleXYPlot.setRangeBoundaries(-25, 25, BoundaryMode.AUTO);
    	mySimpleXYPlot.setDomainBoundaries(0, 10000, BoundaryMode.AUTO);
    	
    	// set del dominio del range e delle label degli assi
    	mySimpleXYPlot.setDomainStepValue(10);
    	mySimpleXYPlot.setTicksPerRangeLabel(3);
    	mySimpleXYPlot.setDomainLabel("Tempo (s)");
    	mySimpleXYPlot.getDomainLabelWidget().pack();
    	mySimpleXYPlot.setRangeLabel("Accelerazione (m/s^2)");
    	mySimpleXYPlot.getRangeLabelWidget().pack();
    	mySimpleXYPlot.disableAllMarkup(); 
    }
   
 /**
  * Metodo che permette la selzione degli assi in funzione delle checkbax selezionate in AccelerometerActivity          
  */
    private void selezioneAssi(){
    	 
          if(AccelerometerActivity.xchecked && !AccelerometerActivity.ychecked && !AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse X", Toast.LENGTH_LONG).show(); 
          	xHistorySeries.setModel(xHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if(!AccelerometerActivity.xchecked && AccelerometerActivity.ychecked && !AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse Y", Toast.LENGTH_LONG).show(); 
          	yHistorySeries.setModel(yHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if(!AccelerometerActivity.xchecked && !AccelerometerActivity.ychecked && AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse Z", Toast.LENGTH_LONG).show(); 
          	zHistorySeries.setModel(zHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if(AccelerometerActivity.xchecked && AccelerometerActivity.ychecked && !AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse X e Y", Toast.LENGTH_LONG).show(); 
          	xHistorySeries.setModel(xHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          	yHistorySeries.setModel(yHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if(AccelerometerActivity.xchecked && !AccelerometerActivity.ychecked && AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse X e Z", Toast.LENGTH_LONG).show(); 
          	xHistorySeries.setModel(xHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          	zHistorySeries.setModel(zHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if(!AccelerometerActivity.xchecked && AccelerometerActivity.ychecked && AccelerometerActivity.zchecked){
          	Toast.makeText(Grafico.this, "Selezionato Asse Y e Z", Toast.LENGTH_LONG).show(); 
          	yHistorySeries.setModel(yHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          	zHistorySeries.setModel(zHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
          }
          
          if (AccelerometerActivity.xchecked && AccelerometerActivity.ychecked && AccelerometerActivity.zchecked){
        	   	Toast.makeText(Grafico.this, "Hai selezionato tutti gli assi ", Toast.LENGTH_LONG).show(); 
        	   	xHistorySeries.setModel(xHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
        	   	yHistorySeries.setModel(yHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
        	   	zHistorySeries.setModel(zHistoryList, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED); 
           }
            
          
          mySimpleXYPlot.redraw();
    }
    
    
    
      
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
                
    }

                

}
